package cn.huangsm.advance.mongodb;

import cn.huangsm.advance.common.utils.SpringUtil;
import cn.huangsm.advance.ratelimiter.RateLimitAspect;
import cn.huangsm.advance.ratelimiter2.AccessRateLimiter;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huang
 * @PACKAGE_NAME cn.huangsm.advance.mongodb
 * @PROJECT_NAME advance-code
 * @date 2019/1/24
 */
@RestController
public class MongoDbController {
    @Resource(name = "mongoUserService")
    private UserService userService;

    @GetMapping("/{_id}")
    public User findUserById(@PathVariable("_id") String id){
        return userService.findUserById(id);
    }

    @PostMapping("/")
    public void save(@RequestBody User user){
        userService.save(user);
    }

    @PutMapping("/")
    public void  update(@RequestBody User user){
        userService.update(user);
    }

    @DeleteMapping("/{id}")
    public void del(@PathVariable("id")String id){
        userService.del(id);
    }

    @GetMapping("/")
    @AccessRateLimiter(limitNum = 5.0)
    public List<User> list(){
        UserService bean = SpringUtil.getBean(UserService.class);
        System.out.println(bean);
        return userService.list();
    }
}
